﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Web;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 外部接口封装：创建CRM发运信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StockInventoryCloseController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_test;


        public StockInventoryCloseController(ILogger<StockInventoryCloseRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_test = _configuration.GetConnectionString("WMSDbConnection_Test");
            _logger = loggerFactory.CreateLogger<StockInventoryCloseController>();

        }

        /// <summary>
        ///  外部接口封装：创建CRM发运信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddLogistics([FromBody] StockInventoryCloseRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            StockInventoryCloseResponseDto re = new StockInventoryCloseResponseDto();



            string factory_code = strparm.factory_code;
            string con_u = "";
            if (factory_code=="C01")
            {
                con_u = con_zh;
            }else if (factory_code == "B01")
            {
                con_u = con_qh;
            }else if (factory_code == "E01")
            {
                con_u = con_test;
            }
            else if(factory_code == "Test")
            {
                con_u = con_test;
            }
            else
            {
                strMsg = "基地编码";
            }







            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("function_description", "新增物流信息");//查看URL配置表地址描述
                string  sql = MethodHelp.GetTranSQL("TMS0015", dic, con);//获取URL地址
                if (sql == "")
                {
                    strMsg = "SYS0017,Parameter conversion failed";
                }
                else
                {
                    strSqlList.Add(sql.ToString());
                }
                if (strMsg == "")
                {
                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A)
                        {

                        }
                        else
                        {
                            strMsg = "HandleTaskUpdate  Database update failed" + "SQL:" + sql;
                        }

                    }
                    else
                    {
                        strMsg = "No insertion data present";
                    }
                }





            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }



            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }

      
    }
}
